using UnityEngine;

namespace Gj
{
    public static class TipTools
    {
        private const string key = "TipTools-indexKey";
        private static readonly string[] list;

        static TipTools()
        {
            var settings = LoadSettings();
            list = settings.tipItems;
        }

        private static TipSettings LoadSettings()
        {
            var loadedSettings = (TipSettings)Resources.Load("TipSettings", typeof(TipSettings));
            if (loadedSettings == null)
            {
                loadedSettings = ScriptableObject.CreateInstance<TipSettings>();
                Debug.LogError("TipSettings is not exist (Resources)");
            }

            return loadedSettings;
        }

        public static string Get()
        {
            var index = GetIndex();
            return list[index];
        }

        public static string Next()
        {
            var index = GetIndex();
            index += 1;
            if (index >= list.Length) index = 0;
            SetIndex(index);
            return list[index];
        }

        private static int GetIndex()
        {
            return CacheTools.GetVal(key, 0);
        }

        private static void SetIndex(int index)
        {
            CacheTools.SetVal(key, index);
        }
    }
}